package unipd.esp1112.pro;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;


/**
 * Initial activity where the user select his profile or creates a new one
 * 
 * @author andrea basso, federico lancerin, alex tomasello
 * 
 */
public class SelezioneProfili extends Activity {
	/**
	 * layout that contains a button for every profile found in the application database
	 */
	private LinearLayout layout;
	/**
	 * variable used to retrieve an instance of the application private database
	 */
	private MyOpenHelper op;
	/**
	 * variable used to reference the database
	 */
	private SQLiteDatabase db;
	/**
	 * int where is stored the width of the display where the application is currently running
	 */
	private int width;
	/**
	 * cursor used to retrieve the results of a query
	 */
	private Cursor query;
	/**
	 * button created for every profile found in the database, it leads to the main menu
	 */
	private Button oldProfile;
	/**
	 * button that leads to the activity where the user can create his new profile
	 */
	private Button newProfile;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profileselection);
		// inizializzo la variabile width contenente la larghezza effettiva
		// dello schermo su cui sto facendo eseguire l'applicazione
		Display display = getWindowManager().getDefaultDisplay();
		width = display.getWidth();
	}

	@Override
	protected void onResume() {
		super.onResume();

		layout = (LinearLayout) findViewById(R.id.profileSelectionLayout);
		layout.removeAllViewsInLayout();

		op = new MyOpenHelper(this);
		db = op.getReadableDatabase();
		query = db.rawQuery("SELECT nome FROM Profilo", null);

		// creo un bottone per ogni profilo esistente e lo aggiungo al layout
		while (!query.isLast() && !query.isAfterLast()) {
			query.moveToNext();
			oldProfile = new Button(this);
			oldProfile.setTypeface(null, Typeface.BOLD_ITALIC);
			oldProfile.setBackgroundResource(R.drawable.customcoloredbutton);
			oldProfile.setTextColor(Color.rgb(255, 255, 128));
			oldProfile.setOnClickListener(new ProfileChoose(oldProfile));
			oldProfile.setWidth(width);
			oldProfile.setText(query.getString(0));
			layout.addView(oldProfile);
		}

		db.close();

		// creo un bottone che mi porterà all'activity NuovoProfilo
		newProfile = (Button) findViewById(R.id.nuovoProfilo);
		newProfile.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent myIntent = new Intent(getApplicationContext(), NuovoProfilo.class);
				startActivityForResult(myIntent, 0);
			}
		});
	}

	private class ProfileChoose implements OnClickListener {
		private Button profileChoose;

		private ProfileChoose(Button pC) {
			profileChoose = pC;
		}

		@Override
		public void onClick(View v) {
			// System.out.println(profileChoose.getText());
			Intent myIntent = new Intent(getApplicationContext(), Menu.class);
			myIntent.putExtra("profilo", profileChoose.getText());
			startActivityForResult(myIntent, 0);
		}

	}

}
